﻿using NLog;
using NLog.LayoutRenderers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;

namespace GetMan.Core.NLogRender
{
    [LayoutRenderer("log-class-method")]
    public class LogMethodLayoutRender : LayoutRenderer
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        private string GetCallerMethodName()
        {
            var stackTrace = new StackTrace(false);
            var frames = stackTrace.GetFrames();
            StringBuilder sb = new StringBuilder();
            foreach (var f in frames)
            {
                sb.AppendLine(f.GetMethod()?.Name);
            }
            return sb.ToString();
        }

        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            builder.Append(GetCallerMethodName());
        }
    }
}